import Vue from 'vue'
import Vuex from 'vuex'
// import vuexCache from 'vuex-cache'
Vue.use(Vuex)
// 这里定义初始值
let state = {
    count: 10,
    debug: true,
    dialog: {
        show: false,
    },
    schemas: {},
    history: {},
    upload: {
        imageHost: process.env.VUE_APP_API_HOST + '/upload/images',
        fileHost: process.env.VUE_APP_API_HOST + '/upload/files',
    },
    pipe: {},//用来互通筛选条件
    num: null,//用来互通筛选条件
    dataRefreshEvents: '{}',//监听刷新事件
    mapCenter: [114.08272,22.5464130],  // 城市切换时 地图中心点相应变化
    cityList: [
        {id: 1,name: '北京',position: [116.39704,39.9087390]},
        {id: 2,name: '上海',position: [121.473657,31.2303780]},
        {id: 3,name: '深圳',position: [114.08272,22.5464130]}

    ]
};

const mutations = {
    /**
     * 对event中的对应model进行监控,只要发现值变化,就同时刷新列表
     * @param model model名
     */
    refreshEvents(state, model) {
        //为啥要这么写? 因为如果不提前定义好state的model,无法监听,只能监听整个json了
        let json = JSON.parse(state.dataRefreshEvents)
        if (!json[model])
            json[model] = 0;
        json[model]++;
        json = JSON.stringify(json);
        state.dataRefreshEvents = json;
    },

    pipe(state, payload) {
        Vue.set(state.pipe, payload.name, payload.value)
    },
    refreshDialog(state) {
        state.num++;
    }

};

// 事件触发后的逻辑操作
// 参数为事件函数
const actions = {};

const getters = {
    //用来互通数据
    getPipe: (state) => (model) => {
        return state.pipe[model];
    },
    //判定表格是不是需要刷新
    getRefreshEvents: (state) => (model) => {
        let json = JSON.parse(state.dataRefreshEvents);
        return json[model];
    }
};

export default new Vuex.Store({
    state,
    // plugins: [vuexCache],
    mutations,
    actions,
    getters
})
